package com.example.chao.myphotosearch.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.chao.myphotosearch.R;
import com.example.chao.myphotosearch.adapters.CycleViewPagerAdapter;
import com.example.chao.myphotosearch.dao.RecommendContent;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chao on 2016/4/25.
 */
public class MyCycleViewRelativeLayout extends RelativeLayout {
    @Bind(R.id.view_cycle)
    ViewPager viewCycle;
    @Bind(R.id.point_container_LL)
    LinearLayout pointContainerLL;
    private int lastPosition;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int currentPosition = viewCycle.getCurrentItem();
            currentPosition++;
            viewCycle.setCurrentItem(currentPosition);
            handler.sendEmptyMessageDelayed(0,2000);
        }
    };

    public MyCycleViewRelativeLayout(Context context) {
        this(context, null);
    }

    public MyCycleViewRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyCycleViewRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.my_cycle_view_rl, this, true);
        ButterKnife.bind(this);
    }

    public void startCycle(){
        stopCycle();
        handler.sendEmptyMessageDelayed(0, 2000);
    }

    public void stopCycle(){
        handler.removeCallbacksAndMessages(null);
    }

    public void initCycleData(final List<RecommendContent> list){
        if(list==null||list.isEmpty()){
            return;
        }
        viewCycle.setAdapter(new CycleViewPagerAdapter(list));
        viewCycle.setCurrentItem(list.size()*10000);
        pointContainerLL.removeAllViews();

        lastPosition = 0;
        //动态添加圆点

        for(int i =0;i<list.size();i++){
            ImageView imageView = new ImageView(getContext());
            imageView.setClickable(true);
            imageView.setImageResource(R.drawable.cycle_view_point_selector);
            //父布局是谁就是谁的布局参数
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            if(i>0){
                params.leftMargin = 8;
                imageView.setEnabled(false);
            }
            imageView.setLayoutParams(params);
            pointContainerLL.addView(imageView);
        }
        startCycle();
        viewCycle.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                position = position%list.size();
                pointContainerLL.getChildAt(position).setEnabled(true);
                pointContainerLL.getChildAt(lastPosition).setEnabled(false);
                lastPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewCycle.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //停止消息,
                        stopCycle();//删除所有回调消息
                        break;
                    case MotionEvent.ACTION_UP:
                        startCycle();
                        break;
                }
                return false;
            }
        });
    }
}
